<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="厂区" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['branchCompanyName', validatorRules.branchCompanyName]" placeholder="请输入标题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['controlDetails', validatorRules.controlDetails]" placeholder="请输入标题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开始时间" v-decorator="['beginDate', validatorRules.beginDate]" :trigger-change="true"
                :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束时间" v-decorator="['endDate', validatorRules.endDate]" :trigger-change="true"
                :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['type']" :trigger-change="true" dictCode="gklx"
                placeholder="请选择规则" @change="handleChangeType" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="规则范围" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag v-if="showControlRangeInput === '排放标准'" type="list_multi"
                v-decorator="['controlRange', validatorRules.controlRange]" :trigger-change="true" dictCode="pflx"
                placeholder="请选择排放标准" />
              <j-multi-select-tag v-if="showControlRangeInput === '使用性质'" type="list_multi"
                v-decorator="['controlRange', validatorRules.controlRange]" :trigger-change="true" dictCode="function"
                placeholder="请选择使用性质" />
              <a-input v-if="showControlRangeInput != '排放标准' && showControlRangeInput != '使用性质'"v-decorator="['controlRange', validatorRules.controlRange]" placeholder="请输入规则范围"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="限制时段" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <j-time placeholder="请选择管控时段" v-decorator="['timeFrame']" :trigger-change="true" style="width: 100%" /> -->
              <div>
                <j-time v-model="startTime" :placeholder="'开始时间'" @change="handleTimeChange" style="margin-right: 10px;"
                  format="HH:mm:ss" />
                <span>-</span>
                <j-time v-model="endTime" :placeholder="'结束时间'" @change="handleTimeChange" style="margin-left: 10px;"
                  format="HH:mm:ss" />
              </div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['notice']" rows="4" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['action', validatorRules.action]" :trigger-change="true"
                dictCode="gkdz" placeholder="请选择状态" />
            </a-form-item>
          </a-col> -->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ControlInstallForm',
  components: {
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => { },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      startTime: null,
      endTime: null,
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        branchCompanyName: {
          rules: [
            { required: true, message: '请输入厂区名称!' },
          ]
        },
        controlDetails: {
          rules: [
            { required: true, message: '请输入管控说明!' },
          ]
        },
        beginDate: {
          rules: [
            { required: true, message: '请选择开始时间!' },
          ]
        },
        endDate: {
          rules: [
            { required: true, message: '请选择结束时间!' },
          ]
        },
        action: {
          rules: [
            { required: true, message: '请选择管控动作!' },
          ]
        },
        controlRange: {
          rules: [
            { required: true, message: '请选择管控范围!' },
          ]
        },
      },
      url: {
        add: "/controlInstall/controlInstall/add",
        edit: "/controlInstall/controlInstall/edit",
        queryById: "/controlInstall/controlInstall/queryById"
      },
      showControlRangeInput: '',
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
  },
  methods: {
    // 管控类型选择
    handleChangeType(value) {
      console.log("~~~", value);
      // 根据 type 的值动态决定显示哪种输入组件
      this.showControlRangeInput = value;
      this.form.setFieldsValue({"controlRange":null});
    },
    handleTimeChange() {
      if (this.startTime && this.endTime) {
        const timeFrame = `${this.startTime}-${this.endTime}`;
        // 将时间范围赋值给 timeFrame 字段
        this.form.setFieldsValue({ 'timeFrame': timeFrame });
        // this.$set(this.form, 'timeFrame', timeFrame);
        console.log(timeFrame);
      }
    },
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'controlDetails', 'beginDate', 'endDate', 'action', 'type', 'controlRange', 'timeFrame', 'notice'))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId };
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'controlDetails', 'beginDate', 'endDate', 'action', 'type', 'controlRange', 'timeFrame', 'notice'))
    },
  }
}
</script>